<template>
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar fixed left-arrow title="小智同学" @click-left="$router.back()" />
    <!-- 聊天主体区域 -->
    <div ref="chatlist" class="chat-list">
      <!-- 左侧是机器人小智 -->
      <div v-for="(item,index) in list" :key="index" class="chat-item" :class="item.name === 'xz'? 'left' : 'right'">
        <!-- 按需渲染左侧的聊天 -->
        <!-- template只起包裹性质,并不会渲染真正结构中 -->
        <template v-if="item.name === 'xz'">
          <van-image fit="cover" round src="https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2364920443,4024864561&fm=26&gp=0.jpg" />
          <div class="chat-pao">{{ item.msg }}</div>
        </template>
        <!-- 按需渲染右侧的聊天 -->
        <template v-else>
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image fit="cover" round :src="photo" />
        </template>
      </div>
    </div>

    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span slot="button" style="font-size:12px;color:#999" @click="send">提交</span>
      </van-field>
    </div>
  </div>
</template>

<script>
import { reqGetUserProfile } from '@/api/user'
import io from 'socket.io-client'
export default {
  name: 'UserChat',
  data() {
    return {
      word: '',
      photo: '',
      list: [ // 对话记录
        // { name: 'xz', msg: '你好，我是无所不知的小智同学！', timestamp: Date.now() },
        // { name: 'me', msg: '小智同学, 请问今天下雨吗？', timestamp: Date.now() },
        // { name: 'me', msg: '快帮我查查', timestamp: Date.now() },
        // { name: 'xz', msg: '今天不下雨', timestamp: Date.now() }
      ],
      socket: null,
      timeid: null
    }
  },
  destroyed() {
    // 断开连接
    this.socket.close()
    clearInterval(this.timeid)
  },
  async created() {
    const { data: { data: { photo }}} = await reqGetUserProfile()
    this.photo = photo
    // 1. 建立服务器链接
    this.socket = io('http://localhost:8888')
    // 2. 监听连接状态
    this.socket.on('connect', () => {
      this.$toast.success('会话连接成功')
    })
    this.timeid = setInterval(() => { console.log(123) }, 500)
    // 3. 接收服务器发送的消息
    // this.socket.on('消息类型', ()=>{})
    this.socket.on('message', ({ msg, timestamp }) => {
      // console.log(msg)
      // 将接收到的消息追加给list
      this.list.push({
        name: 'xz',
        msg,
        timestamp
      })
      // bug - chatlist的scrollTop(滚动卷曲的高度)值不会自动修改
      // 使用scrollHeight(元素本身的高度+元素卷曲的高度) 设置给srcollTop
      this.$nextTick(() => {
        this.$refs.chatlist.scrollTop = this.$refs.chatlist.scrollHeight
      })
    })
  },
  methods: {
    send() {
      // console.log(this.word)
      // 向服务发送信息
      if (!this.word) return
      this.socket.emit('message', { msg: this.word, timestamp: Date.now() })
      // 也需要将个人发送信息追加到数组中
      this.list.push({
        name: 'me',
        msg: this.word,
        timestamp: Date.now()
      })
      // 清空输入框
      this.word = ''
      // 更新滚动条
      this.$nextTick(() => {
        this.$refs.chatlist.scrollTop = this.$refs.chatlist.scrollHeight
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background:#fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item{
      padding: 10px;
      .van-image{
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao{
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before{
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top:0.5px solid #c2d9ea;
          border-right:0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right{
  text-align: right;
  .chat-pao{
    margin-left: 0;
    margin-right: 15px;
    &::before{
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left{
  text-align: left;
  .chat-pao{
    margin-left: 15px;
    margin-right: 0;
    &::before{
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
